<?php
namespace app\admin\controller;
use app\admin\logic\LogLogic;
use my\Security;
use my\Tools;
use think\Db;
use app\admin\model\AdminModel;

class UserRechargeLog extends Base
{
    public $map = array();
    public function _initialize()
    {
        parent::_initialize();
        $areaInfo = AdminModel::getRoleArea();
        if(!empty($areaInfo)){
            $this->map['b.register_city'] = ['in',implode(',',$areaInfo['city'])];
        }
        $this->assign('pay_type',pay_type());
    }
    //充值记录
    public function index()
    {
        return view();
    }
	//用户消费流水
    public function flow()
    {
        $typeList = user_flow_type();
        foreach ($typeList as $key=>$value){
            if(!in_array($key,[2,5,6,7,9,11,15,16,18,20])){
                unset($typeList[$key]);
            }
        }
        $this->assign('typeList',$typeList);
        return view();
    }

    //退款流水
    public function refund()
    {
        $typeList = user_flow_type();
        foreach ($typeList as $key=>$value){
            if(!in_array($key,[4,14,17,19,22])){
                unset($typeList[$key]);
            }
        }
        $this->assign('typeList',$typeList);
        return view();
    }
    //异步获取数据方法
	public function table()
    {
        $page   = input('get.page');
        $limit  = input('get.limit');
        $data   = input('get.');
        $map    = self::whereInfo($data);
        $logic  = new LogLogic();
        $list   = $logic->getUserRechargeLog($map, 'a.*,b.user_phone',true,$page, $limit);
        $count  = Db::table('xm_user_recharge a')->join('user b','a.uid=b.uid',"LEFT")->where($map)->count();
        foreach ($list as $key => $value){
            $list[$key]['type']             = pay_type()[$value['type']];               //支付方式
            $list[$key]['user_phone']       = Security::decrypt($value['user_phone']);
            $list[$key]['money']            = $value['money']/100;
            $list[$key]['return_money']     = $value['return_money']/100;
            $list[$key]['gifts_money']      = $value['gifts_money']/100;
            if(!empty($value['status'])){
                $list[$key]['status_name']  = "充值成功";
            }else{
                $list[$key]['status_name']  = "<span style='color: red'>待充值</span>";
            }
            $list[$key]['time']             = $value['time']?date("Y-m-d H:i:s",$value['time']):'';
            $list[$key]['statement_type']   = user_flow_type($value['money_statement']);
        }
        echo json_encode(array(
            'code'  => 0,
            'msg'   => '',
            'limit' => $limit,
            'count' => $count,
            'data'  => $list
        ));
    }

    //导出数据
    public function excel(){
        $data   = input('get.');
        $map    = self::whereInfo($data);
        $list   = Db::name('user_recharge a')->join('user b','a.uid=b.uid',"LEFT")->where($map)->field('a.*,b.user_phone')->order('money_id desc')->select();
        foreach ($list as $key => $value){
            $list[$key]['type']             = pay_type()[$value['type']];               //支付方式
            $list[$key]['user_phone']       = Security::decrypt($value['user_phone']);
            $list[$key]['money']            = $value['money']/100;
            $list[$key]['return_money']     = $value['return_money']/100;
            $list[$key]['gifts_money']      = $value['gifts_money']/100;
            if(!empty($value['status'])){
                $list[$key]['status_name']  = "充值成功";
            }else{
                $list[$key]['status_name']  = "待充值";
            }
            $list[$key]['time']             = $value['time']?date("Y-m-d H:i:s",$value['time']):'';
            $list[$key]['statement_type']   = user_flow_type($value['money_statement']);
        }
        $header = [
            'money_id'      => 'ID',           'uid'            => '用户UID',
            'user_phone'    => '手机号',       'out_trade_no'   => '交易号',
            'money'         => '充值金额',     'return_money'   => '不可提现金额',
            'gifts_money'   => '赠送金额',     'status_name'    => '状态',
            'type'           => '付款方式',    'time'           => '充值时间'
        ];
        foreach ($list as $key => $value) {
            foreach ($header as $k => $v) {
                $result[$key][$v] = $value[$k];
            }
        }
        Tools::excelExport('充值记录', $header, $result);
    }

    //处理 查询条件数据
    public function whereInfo($data = []){
        $map    = $this->map;
        //流水类型
        if(!empty($data['statement']) && $data['statement'] == 'refund'){                 //退款
            $map['money_statement'] = ['in',[4,14,17,19,22]];
        }elseif(!empty($data['statement']) && $data['statement'] == 'flow'){              //消费
            if(!empty($data['money_statement'])){
                $map['money_statement'] = $data['money_statement'];
            }else{
                $map['money_statement'] = ['in',[2,5,6,7,9,11,15,16,18,20]];
            }
        }elseif(!empty($data['statement']) && $data['statement'] == 'recharge'){          //充值
            $map['money_statement'] = 1;
        }
        //手机号搜索
        if(!empty($data['user_phone'])){
            $map['b.user_phone']    = Security::encrypt($data['user_phone']);
        }
        //充值状态
        if($data['status'] != ''){
            $map['a.status']    = $data['status'];
        }
        //充值/消费/退款时间
        if(!empty($data['start']) && empty($data['end'])){     //开始时间
            $map['a.time'] = ['>=',strtotime($data['start'])];
        }elseif(empty($data['start']) && !empty($data['end'])){     //结束时间
            $map['a.time'] = ['<=',strtotime($data['end'])];
        }elseif(!empty($data['start']) && !empty($data['end'])){
            $map['a.time'] = ['between',[strtotime($data['start']),strtotime($data['end'])]];
        }
        //付款方式
        if($data['pay_type'] != ''){
            $map['a.type']    = $data['pay_type'];
        }
        //用户ID
        if(input('get.uid')){
            $map['a.uid'] = input('get.uid');
        }
        $map['b.false_status'] = 0;
        return $map;
    }

    //日志说明
    public function remark()
    {
        $map['money_id'] = input('post.id');
        if(empty($map['money_id'])){
            return array('status'=>0,'msg'=>'参数异常');
        }
        $logic = new LogLogic();
        $res = $logic->getUserRechargeLog($map, 'remark',false);
        return array('status'=>1,'data'=>$res['remark']);
    }

}
